package cn.tedu.ucenter.controller;


import cn.tedu.commonUtils.JwtUtils;
import cn.tedu.commonUtils.R;
import cn.tedu.serviceBase.exception.EducationException;
import cn.tedu.ucenter.entity.vo.RegisterVo;
import cn.tedu.ucenter.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.tedu.ucenter.entity.Member;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-05-23
 */
@RestController
@CrossOrigin
@RequestMapping("/ucenter/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    @GetMapping("/{id}")
    public Map<String,Object> getMemberById(@PathVariable String id){
        Member member = memberService.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("nickName",member.getNickname());
        map.put("avatar",member.getAvatar());
        return map;
    }

    @PostMapping("/login")
    public R login(@RequestBody Member member){
        return memberService.login(member);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("/auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            Member member = memberService.getById(memberId);
            return R.ok().data("item", member);
        }catch (Exception e){
            e.printStackTrace();
            throw new EducationException(20001,"error");
        }
    }

}

